--package.path = package.path..";?.lua"
package.path = "./plugin/HP1101H/utils.lua"..";..\\?.lua"
package.path = "./plugin/HP1101H/json.lua"..";..\\?.lua"

require ("utils")
require ("json")

-- --rxBuf = {0x7E,0x32,0x30,0x30,0x31,0x36,0x30,0x30,0x30,0x44,
--          0x30,0x31,0x32,0x31,0x31,0x30,0x33,0x45,0x38,0x30,
--          0x34,0x42,0x30,0x35,0x43,0x33,0x30,0x30,0x32,0x35,
--          0x38,0x46,0x39,0x44,0x45,0x0D}
rxBuf = {}
RegAddr = 0x01

function NewVariables()
    -- body
    local VariableMap = {
        Variable = {
        },
    }

    return VariableMap
end

function GenerateGetRealVariables(sAddr,step)

    -- body
    local requestADU = {}
    local requestADULen = 0
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(step == 0)
    then
        ----------查询系统实时数据和状态-----------------
        requestADU[1] = 0x51
        requestADU[2] = 0x36
        requestADU[3] = 0x0D

        requestADULen = 3

        RegAddr = 0x01

        VariableMap = {
            Status = "0",
            Variable = requestADU
        }     
    end

    -- local tmp = ""
    -- for i=1,requestADULen,1 do
    --     tmp = tmp .. " " .. string.format("%02X",requestADU[i])
    -- end
    -- print(tmp)
    return VariableMap
end

function DeviceCustomCmd(sAddr,cmdName,cmdParam,step)

    -- body
    local requestADU = {}
    local requestADULen = 0
    local requestPDU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if(cmdName == "GetRealVariables")
    then
        if(step == 0)
        then
            ----------查询系统实时数据和状态-----------------
            requestADU[1] = 0x51
            requestADU[2] = 0x36
            requestADU[3] = 0x0D

            requestADULen = 3

            RegAddr = 0x01

            VariableMap = {
                Status = "0",
                Variable = requestADU
            }     
        end
    elseif(cmdName == "SetOpen")
    then
        if(step == 0)
        then
            ----------遥控空调开机-----------------
            requestPDU[1] = 0x20
            requestPDU[2] = sAddr
            requestPDU[3] = 0x60
            requestPDU[4] = 0x45
            local LENGTHBuf = ydt1363.GetLENGTHHex(0x02)
            requestPDU[5] = LENGTHBuf[1]
            requestPDU[6] = LENGTHBuf[2]
            requestPDU[7] = 0x10

            --获取校验和码
            local requestPDULen = 7
            local chksum = ydt1363.GetCHKSUM(requestPDU,requestPDULen)

            --往缓存中加入SOI
            table.insert(requestADU,0x7E)
            requestADULen = requestADULen + 1

            --往缓存中加入VER~INFO数据
            local requestPDUStr = {}
            requestPDUStr = utils.Hex2Str(requestPDU,requestPDULen)
            for index=1,2*requestPDULen,1 do
                table.insert(requestADU,requestPDUStr[index])
            end
            requestADULen = requestADULen + 2*requestPDULen

            --往缓存中加入CHKSUM
            for index=1,4,1 do
                table.insert(requestADU,chksum[index])
            end
            requestADULen = requestADULen + 4

            --往缓存中加入EOI
            table.insert(requestADU,0x0D)
            requestADULen = requestADULen + 1

            regAddr = 0x6045
            VariableMap = {
                Status = "0",
                Variable = requestADU
            }
        end                     
    end

    return VariableMap
end

function AnalysisRx(sAddr,rxBufCnt)

    local SOI,VER,ADR,CID1,CID2,LENGTH,LENID,INFO,DATA,CHKSUM,EOI

    -- body
    local VariableMap = {
        Status = "1",
        Variable = {},
    }

    if(rxBufCnt < 110)
    then
        rxBuf = {}
        return VariableMap
    end


    for index=1,rxBufCnt,1 do
        if(index <= rxBufCnt)
        then
            if((index+109) > rxBufCnt) then
                rxBuf = {}
                VariableMap = {
                    Status = "1",
                    Variable = {}
                }
                return VariableMap  
            end
            
            --判断帧头和帧尾
            if((rxBuf[index]==0x28) and (rxBuf[index+109] == 0x0D)) then
                local Variable = {}
                local VariableIndex = 1
                local DATAIndex = index + 1
                --查询系统实时数据和状态
                if(RegAddr == 0x01) then
                    --------------------------三相电压R------------------------------------------------
                    local InVoltageR = (rxBuf[DATAIndex]-0x30)*1000+(rxBuf[DATAIndex+1]-0x30)*100+
                                    (rxBuf[DATAIndex+2]-0x30)*10+(rxBuf[DATAIndex+4]-0x30)
                    DATAIndex = DATAIndex + 5 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"InVoltageR",InVoltageR,InVoltageR)
                    VariableIndex = VariableIndex + 1
                    --------------------------三相电压S------------------------------------------------
                    local InVoltageS = (rxBuf[DATAIndex]-0x30)*1000+(rxBuf[DATAIndex+1]-0x30)*100+
                                    (rxBuf[DATAIndex+2]-0x30)*10+(rxBuf[DATAIndex+4]-0x30)
                    DATAIndex = DATAIndex + 5 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"InVoltageS",InVoltageS,InVoltageS)
                    VariableIndex = VariableIndex + 1
                    --------------------------三相电压T------------------------------------------------
                    local InVoltageT = (rxBuf[DATAIndex]-0x30)*1000+(rxBuf[DATAIndex+1]-0x30)*100+
                                    (rxBuf[DATAIndex+2]-0x30)*10+(rxBuf[DATAIndex+4]-0x30)
                    DATAIndex = DATAIndex + 5 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"InVoltageT",InVoltageT,InVoltageT)
                    VariableIndex = VariableIndex + 1
                    --------------------------输入频率------------------------------------------------
                    local InFreq = (rxBuf[DATAIndex]-0x30)*100+(rxBuf[DATAIndex+1]-0x30)*10+(rxBuf[DATAIndex+3])
                    DATAIndex = DATAIndex + 4 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"InFreq",InFreq,InFreq)
                    VariableIndex = VariableIndex + 1
                    --------------------------三相输出电压R------------------------------------------------
                    local OutVoltageR = (rxBuf[DATAIndex]-0x30)*1000+(rxBuf[DATAIndex+1]-0x30)*100+
                    (rxBuf[DATAIndex+2]-0x30)*10+(rxBuf[DATAIndex+4]-0x30)
                    DATAIndex = DATAIndex + 5 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutVoltageR",OutVoltageR,OutVoltageR)
                    VariableIndex = VariableIndex + 1
                    --------------------------三相输出电压S------------------------------------------------
                    local OutVoltageS = (rxBuf[DATAIndex]-0x30)*1000+(rxBuf[DATAIndex+1]-0x30)*100+
                                    (rxBuf[DATAIndex+2]-0x30)*10+(rxBuf[DATAIndex+4]-0x30)
                    DATAIndex = DATAIndex + 5 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutVoltageS",OutVoltageS,OutVoltageS)
                    VariableIndex = VariableIndex + 1
                    --------------------------三相输出电压T------------------------------------------------
                    local OutVoltageT = (rxBuf[DATAIndex]-0x30)*1000+(rxBuf[DATAIndex+1]-0x30)*100+
                                    (rxBuf[DATAIndex+2]-0x30)*10+(rxBuf[DATAIndex+4]-0x30)
                    DATAIndex = DATAIndex + 5 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutVoltageT",OutVoltageT,OutVoltageT)
                    VariableIndex = VariableIndex + 1
                    --------------------------输出频率------------------------------------------------
                    local OutFreq = (rxBuf[DATAIndex]-0x30)*100+(rxBuf[DATAIndex+1]-0x30)*10+(rxBuf[DATAIndex+3])
                    DATAIndex = DATAIndex + 4 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutFreq",OutFreq,OutFreq)
                    VariableIndex = VariableIndex + 1
                    --------------------------三相输出电流R------------------------------------------------
                    local OutCurrentR = (rxBuf[DATAIndex]-0x30)*100+(rxBuf[DATAIndex+1]-0x30)*10+(rxBuf[DATAIndex+2]-0x30)
                    DATAIndex = DATAIndex + 4 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutVoltageR",OutVoltageR,OutVoltageR)
                    VariableIndex = VariableIndex + 1
                    --------------------------三相输出电流S------------------------------------------------
                    local OutCurrentS = (rxBuf[DATAIndex]-0x30)*100+(rxBuf[DATAIndex+1]-0x30)*10+(rxBuf[DATAIndex+2]-0x30)
                    DATAIndex = DATAIndex + 4 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutCurrentS",OutCurrentS,OutCurrentS)
                    VariableIndex = VariableIndex + 1
                    --------------------------三相输出电流T------------------------------------------------
                    local OutCurrentT = (rxBuf[DATAIndex]-0x30)*100+(rxBuf[DATAIndex+1]-0x30)*10+(rxBuf[DATAIndex+2]-0x30)
                    DATAIndex = DATAIndex + 4 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"OutCurrentT",OutCurrentT,OutCurrentT)
                    VariableIndex = VariableIndex + 1
                    --------------------------电池电压SSS.S------------------------------------------------
                    local Positive = (rxBuf[DATAIndex]-0x30)*1000+(rxBuf[DATAIndex+1]-0x30)*100+
                    (rxBuf[DATAIndex+2]-0x30)*10+(rxBuf[DATAIndex+4]-0x30)
                    DATAIndex = DATAIndex + 5 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"Positive",Positive,Positive)
                    VariableIndex = VariableIndex + 1
                    --------------------------电池电压VVV.V------------------------------------------------
                    local Negative = (rxBuf[DATAIndex]-0x30)*1000+(rxBuf[DATAIndex+1]-0x30)*100+
                    (rxBuf[DATAIndex+2]-0x30)*10+(rxBuf[DATAIndex+4]-0x30)
                    DATAIndex = DATAIndex + 5 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"Negative",Negative,Negative)
                    VariableIndex = VariableIndex + 1
                    -------------------------- 温度TT.T------------------------------------------------
                    local Temperature = (rxBuf[DATAIndex]-0x30)*100+(rxBuf[DATAIndex+1]-0x30)*10+
                    (rxBuf[DATAIndex+3]-0x30)
                    DATAIndex = DATAIndex + 4 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"Temperature",Temperature,Temperature)
                    VariableIndex = VariableIndex + 1
                    -------------------------- 温度TT.T------------------------------------------------
                    local Temperature = (rxBuf[DATAIndex]-0x30)*100+(rxBuf[DATAIndex+1]-0x30)*10+
                    (rxBuf[DATAIndex+3]-0x30)
                    DATAIndex = DATAIndex + 4 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"Temperature",Temperature,Temperature)
                    VariableIndex = VariableIndex + 1
                    -------------------------- 剩余时间------------------------------------------------
                    local RemainingTime = (rxBuf[DATAIndex]-0x30)*10000+(rxBuf[DATAIndex+1]-0x30)*1000+
                    (rxBuf[DATAIndex+2]-0x30)*100+(rxBuf[DATAIndex+3]-0x30)*10+(rxBuf[DATAIndex+4]-0x30)
                    DATAIndex = DATAIndex + 5 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"RemainingTime",RemainingTime,RemainingTime)
                    VariableIndex = VariableIndex + 1
                    -------------------------- 电池容量------------------------------------------------
                    local Capacity = (rxBuf[DATAIndex]-0x30)*100+(rxBuf[DATAIndex+1]-0x30)*10+
                    (rxBuf[DATAIndex+2]-0x30)
                    DATAIndex = DATAIndex + 3 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"Capacity",Capacity,Capacity)
                    VariableIndex = VariableIndex + 1
                    -------------------------- 系统模式------------------------------------------------
                    local SystemModel = (rxBuf[DATAIndex]-0x30)
                    DATAIndex = DATAIndex + 1 
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"SystemModel",SystemModel,SystemModel)
                    VariableIndex = VariableIndex + 1
                    -------------------------- 电池测试状态------------------------------------------------
                    local BatteryTestStatus = (rxBuf[DATAIndex]-0x30)
                    DATAIndex = DATAIndex + 2 --需要再加一个空格
                    Variable[VariableIndex] = utils.AppendVariable(VariableIndex,"BatteryTestStatus",BatteryTestStatus,BatteryTestStatus)
                    VariableIndex = VariableIndex + 1
                    -------------------------- 故障代码------------------------------------------------
                    DATAIndex = DATAIndex + 9 --需要再加一个空格
                    -------------------------- 警告------------------------------------------------
                    DATAIndex = DATAIndex + 9 --需要再加一个空格
                    -------------------------- 输出变压器类型------------------------------------------------
                    DATAIndex = DATAIndex + 1 --需要再加一个空
                    -------------------------- 输出线电压或相电压------------------------------------------------
                    DATAIndex = DATAIndex + 2 --需要再加一个空格

                    VariableMap = {
                        Status = "0",
                        Variable = Variable
                    }
                    rxBuf = {}
                    return VariableMap     
                end
            else
                index = index + 1
            end
        else
            rxBuf = {}
            return VariableMap
        end
    end

    rxBuf = {}
    return VariableMap
end

--GenerateGetRealVariables(1,0)
--AnalysisRx(1,36)